package com.bolt.report.engine.model;

import com.bolt.common.collection.IteratorUtil;
import com.bolt.report.definition.*;
import com.bolt.report.engine.BRGroupField;
import com.bolt.report.engine.BRSortField;

import java.util.*;

public class ReportPrint {

    private String name;

    private Integer topMargin = 0;

    private Integer leftMargin = 0;

    private Integer bottomMargin = 0;

    private Integer rightMargin = 0;

    private Map<String, StyleDefinition> stylesMap = new LinkedHashMap<>();

    private Title title;

    private ColumnHeader columnHeader;

    private List<PageDetails> pages;

    private Summary summary;

    private List<BRGroupField> group;

    private List<BRSortField> sortFields;

    private PrintOrder printOrderValue;

    private PaperDefinition paper;

    protected boolean ignorePagination;

    private String queryFormHtml;

    public ReportPrint() {
        this(new ArrayList<>());
    }

    public ReportPrint(List<PageDetails> pages) {
        this.pages = Collections.synchronizedList(pages);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPageWidth(PaperSize.SizeUnit unit) {
        return this.printOrderValue == PrintOrder.VERTICAL
                ? paper.getWidth(unit) : paper.getHeight(unit);
    }


    public float getPageHeight(PaperSize.SizeUnit unit) {
        return this.printOrderValue == PrintOrder.VERTICAL
                ? paper.getHeight(unit) : paper.getWidth(unit);
    }

    public Integer getTopMargin() {
        return topMargin;
    }

    public void setTopMargin(Integer topMargin) {
        this.topMargin = topMargin;
    }

    public Integer getLeftMargin() {
        return leftMargin;
    }

    public void setLeftMargin(Integer leftMargin) {
        this.leftMargin = leftMargin;
    }

    public Integer getBottomMargin() {
        return bottomMargin;
    }

    public void setBottomMargin(Integer bottomMargin) {
        this.bottomMargin = bottomMargin;
    }

    public Integer getRightMargin() {
        return rightMargin;
    }

    public void setRightMargin(Integer rightMargin) {
        this.rightMargin = rightMargin;
    }

    public Map<String, StyleDefinition> getStylesMap() {
        return stylesMap;
    }

    public void setStylesMap(Map<String, StyleDefinition> stylesMap) {
        this.stylesMap = stylesMap;
    }


    public Title getTitle() {
        return title;
    }

    public void setTitle(Title title) {
        this.title = title;
    }

    public Summary getSummary() {
        return summary;
    }

    public void setSummary(Summary summary) {
        this.summary = summary;
    }

    public List<BRSortField> getSortFields() {
        return sortFields;
    }

    public void setSortFields(List<BRSortField> sortFields) {
        this.sortFields = sortFields;
    }

    public boolean haveGroupField() {
        return IteratorUtil.isNotEmpty(this.getGroup());
    }

    public List<BRGroupField> getGroup() {
        return group;
    }

    public void setGroup(List<BRGroupField> group) {
        this.group = group;
    }

    public List<BRGroupField> getGroupFields() {
        if (null != this.getGroup()) {
            return this.getGroup();
        } else {
            return Collections.emptyList();
        }
    }

    public ColumnHeader getColumnHeader() {
        return columnHeader;
    }

    public void setColumnHeader(ColumnHeader columnHeader) {
        this.columnHeader = columnHeader;
    }


    public PrintOrder getPrintOrderValue() {
        return printOrderValue;
    }

    public void setPrintOrderValue(PrintOrder printOrderValue) {
        this.printOrderValue = printOrderValue;
    }

    public PaperDefinition getPaper() {
        return paper;
    }

    public void setPaper(PaperDefinition paper) {
        this.paper = paper;
    }

    public void setPages(List<PageDetails> pages) {
        this.pages = pages;
    }

    public List<PageDetails> getPages() {
        return pages;
    }

    public synchronized void addPage(PageDetails page) {
        pages.add(page);
    }

    public synchronized void addPage(int index, PageDetails page) {
        pages.add(index, page);
    }

    public void removeLastEmptyPage() {
        if (pages.size() > 0) {
            PageDetails last = pages.get(pages.size() - 1);
            if (last.getCurrentRowSize() == 0) {
                pages.remove(last);
            }
        }
    }

    public boolean isIgnorePagination() {
        return ignorePagination;
    }

    public void setIgnorePagination(boolean ignorePagination) {
        this.ignorePagination = ignorePagination;
    }

    public String getQueryFormHtml() {
        return queryFormHtml;
    }

    public void setQueryFormHtml(String queryFormHtml) {
        this.queryFormHtml = queryFormHtml;
    }
}